// pages/index/zone/goodsDetail/appraise/publish/.publish.js
import { axios } from '../../../../../utils/ajax.js'
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploadimg:[],
    starmark: 0,
    goods_id:'',
    data:'',
    userid:'',
    token:'',
    boId:'',
    iPhoneX:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.model)
    if (app.globalData.model.substr(0, 8) == 'iPhone X') {
      this.setData({
        iPhoneX: true
      })
    }
    let { userid = '', token = '' } = wx.getStorageSync('userinfo') 
    this.setData({
      userid: userid,
      token: token
    })
    //options有数据 说明从订单列表中进来，携带商品goodsid
    if (options.goods_id){
      console.log(options.goods_id)
      this.setData({
        goods_id: options.goods_id
      })
    }
    axios({
      url: '/bask/baskgoodslist',
      data: {
        userid: userid,
        token:token,
        goods_id: this.data.goods_id,
        page:1
      }
    }).then((res)=>{
      console.log(res)
      this.setData({
        data: res.data.goodsInfo[0]    //res.data.goods[0]
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //提交按钮
  confirmmeg (e) {
    let usercontent = e.detail.value.usercontent
    let data = this.data.data
    let { userid = '', token = '' } = wx.getStorageSync('userinfo') 
    let score = (parseInt(this.data.starmark) + 1) * 20
    console.log(score)
    //评论没写
    if (!usercontent){
      wx.showToast({
        title: '请填写评论',
        icon:"none"
      })
    }else{
      //上传信息（不包括相册）拿到晒单id 
      let time =  new Date().getTime()/1000
      axios({
        url: `/bask/publishBask/token/${token}/userid/${userid}`,
        data:{
          time: time,
          comment: usercontent,
          color: data.color,
          order_no: data.orderNo,
          skusize: data.skusize,
          goodsId: data.goodsId,
          specId: data.specId,
          score: score,
          goodsName: data.goodsName
        },
        method: 'POST'
      }).then((res)=>{
        wx.showToast({
          title: res.data.message,
          icon:"none"
        })
        console.log(res)
        this.setData({ 
          boId: res.data.boId
        })
        //如果上传了图片
        if (this.data.uploadimg.length){
          //循环上传相册
          this.data.uploadimg.forEach((value, index) => {
            wx.uploadFile({
              url: `${app.host}/Bask/publishBaskImage/token/${this.data.token}/bo_id/${this.data.boId}/userid/${this.data.userid}`,
              filePath: this.data.uploadimg[index],
              name: 'uploadfiles',
              formData: {
                userid: this.data.userid
              },
              success: function (res) {
                console.log(res)
              }
            })
          }) 
        }
        setTimeout(()=>{
          wx.navigateBack({
            delta:1
          })
        },1500)
      })
    }
  },
  //上传图片
  uploadimg () {
    wx.chooseImage({
      count: app.globalData.maxuploadNumber, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: (res)=>{
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let obj = this.data.uploadimg
        res.tempFilePaths.forEach((val)=>{
          obj.push(val)
        })
        this.setData({
          uploadimg: obj
        })
      }
    })
  },
  //选择星级
  choosestar (e) {
    let index = e.currentTarget.dataset.index
    console.log(index)
    this.setData({
      starmark: index
    })
  },
  //跳到选择评价商品列表
  collectachievement (e) {
    let goodsid = e.currentTarget.dataset.goodsid
    console.log(goodsid)
    wx.navigateTo({
      url: `achieventlist/achieventlist?goodsid=${goodsid}`,
    })
  }
})